package com.aaa.controller;

import com.aaa.entity.Products;
import com.aaa.entity.Productstype;
import com.aaa.service.ProductsService;
import com.aaa.util.DefaultMsg;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/products")
public class ProductsController {
    @Autowired
    ProductsService productsService;
    @RequestMapping("/selectAll")
    public PageInfo selectAll(String pname, Integer currentPage, Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<Products> list = productsService.selectAll(pname);
        PageInfo<Products> pageInfo=new PageInfo<Products>(list);
        return pageInfo;
    }
    @RequestMapping("/saveOrUpdate")
    public DefaultMsg saveOrUpdate(Products products){
        DefaultMsg defaultMsg=new DefaultMsg();
        if (products.getPid()==null){
            products.setSku(0);
        }

        boolean b = productsService.saveOrUpdate(products);
        if (!b){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("操作失败");
            defaultMsg.setCode(500);
        }
        return defaultMsg;
    }
    @RequestMapping("/updateStatus1")
    public DefaultMsg updateStatus1(Integer pid){
        DefaultMsg defaultMsg=new DefaultMsg();
        Products products=new Products();
        products.setPid(pid);
        products.setStatus(1);
        boolean b = productsService.saveOrUpdate(products);
        if (!b){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("操作失败");
            defaultMsg.setCode(500);
        }
        return defaultMsg;
    }
    @RequestMapping("/updateStatus2")
    public DefaultMsg updateStatus2(Integer pid){
        DefaultMsg defaultMsg=new DefaultMsg();
        Products products=new Products();
        products.setPid(pid);
        products.setStatus(0);
        boolean b = productsService.saveOrUpdate(products);
        if (!b){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("操作失败");
            defaultMsg.setCode(500);
        }
        return defaultMsg;
    }
    @RequestMapping("delete")
    public DefaultMsg delete(Integer pid){
        DefaultMsg defaultMsg=new DefaultMsg();
        boolean b = productsService.removeById(pid);
        if (!b){
            defaultMsg.setSuccess(0);
            defaultMsg.setError("操作失败");
            defaultMsg.setCode(500);
        }
        return defaultMsg;
    }
    @RequestMapping("/upload")
    public DefaultMsg upload(MultipartFile file){
        DefaultMsg defaultMsg=new DefaultMsg();
        String dir="D:/upload_374/head/";
        String saveName= UUID.randomUUID().toString();
        File saveFile=new File(dir+saveName);
        try {
            file.transferTo(saveFile);
            defaultMsg.setTarget(saveName);
        } catch (IOException e) {
            e.printStackTrace();
            defaultMsg.setSuccess(0);
            defaultMsg.setError("上传失败");
        }
        return defaultMsg;
    }
    @RequestMapping("/selectProductsType")
    public List<Productstype> selectProductsType(){
        return productsService.selectAllType();
    }
}
